/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Controladores;

import Controladores.exceptions.IllegalOrphanException;
import Controladores.exceptions.NonexistentEntityException;
import Entidades.Modelo;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Transporte;
import java.util.ArrayList;
import java.util.Collection;

/**
 *
 * @author Anali
 */
public class ModeloJpaController {

    public ModeloJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Modelo modelo) {
        if (modelo.getTransporteCollection() == null) {
            modelo.setTransporteCollection(new ArrayList<Transporte>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Transporte> attachedTransporteCollection = new ArrayList<Transporte>();
            for (Transporte transporteCollectionTransporteToAttach : modelo.getTransporteCollection()) {
                transporteCollectionTransporteToAttach = em.getReference(transporteCollectionTransporteToAttach.getClass(), transporteCollectionTransporteToAttach.getIdTransporte());
                attachedTransporteCollection.add(transporteCollectionTransporteToAttach);
            }
            modelo.setTransporteCollection(attachedTransporteCollection);
            em.persist(modelo);
            for (Transporte transporteCollectionTransporte : modelo.getTransporteCollection()) {
                Modelo oldModeloOfTransporteCollectionTransporte = transporteCollectionTransporte.getModelo();
                transporteCollectionTransporte.setModelo(modelo);
                transporteCollectionTransporte = em.merge(transporteCollectionTransporte);
                if (oldModeloOfTransporteCollectionTransporte != null) {
                    oldModeloOfTransporteCollectionTransporte.getTransporteCollection().remove(transporteCollectionTransporte);
                    oldModeloOfTransporteCollectionTransporte = em.merge(oldModeloOfTransporteCollectionTransporte);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Modelo modelo) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Modelo persistentModelo = em.find(Modelo.class, modelo.getIdmodelo());
            Collection<Transporte> transporteCollectionOld = persistentModelo.getTransporteCollection();
            Collection<Transporte> transporteCollectionNew = modelo.getTransporteCollection();
            List<String> illegalOrphanMessages = null;
            for (Transporte transporteCollectionOldTransporte : transporteCollectionOld) {
                if (!transporteCollectionNew.contains(transporteCollectionOldTransporte)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Transporte " + transporteCollectionOldTransporte + " since its modelo field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Transporte> attachedTransporteCollectionNew = new ArrayList<Transporte>();
            for (Transporte transporteCollectionNewTransporteToAttach : transporteCollectionNew) {
                transporteCollectionNewTransporteToAttach = em.getReference(transporteCollectionNewTransporteToAttach.getClass(), transporteCollectionNewTransporteToAttach.getIdTransporte());
                attachedTransporteCollectionNew.add(transporteCollectionNewTransporteToAttach);
            }
            transporteCollectionNew = attachedTransporteCollectionNew;
            modelo.setTransporteCollection(transporteCollectionNew);
            modelo = em.merge(modelo);
            for (Transporte transporteCollectionNewTransporte : transporteCollectionNew) {
                if (!transporteCollectionOld.contains(transporteCollectionNewTransporte)) {
                    Modelo oldModeloOfTransporteCollectionNewTransporte = transporteCollectionNewTransporte.getModelo();
                    transporteCollectionNewTransporte.setModelo(modelo);
                    transporteCollectionNewTransporte = em.merge(transporteCollectionNewTransporte);
                    if (oldModeloOfTransporteCollectionNewTransporte != null && !oldModeloOfTransporteCollectionNewTransporte.equals(modelo)) {
                        oldModeloOfTransporteCollectionNewTransporte.getTransporteCollection().remove(transporteCollectionNewTransporte);
                        oldModeloOfTransporteCollectionNewTransporte = em.merge(oldModeloOfTransporteCollectionNewTransporte);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = modelo.getIdmodelo();
                if (findModelo(id) == null) {
                    throw new NonexistentEntityException("The modelo with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Modelo modelo;
            try {
                modelo = em.getReference(Modelo.class, id);
                modelo.getIdmodelo();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The modelo with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Transporte> transporteCollectionOrphanCheck = modelo.getTransporteCollection();
            for (Transporte transporteCollectionOrphanCheckTransporte : transporteCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Modelo (" + modelo + ") cannot be destroyed since the Transporte " + transporteCollectionOrphanCheckTransporte + " in its transporteCollection field has a non-nullable modelo field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(modelo);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Modelo> findModeloEntities() {
        return findModeloEntities(true, -1, -1);
    }

    public List<Modelo> findModeloEntities(int maxResults, int firstResult) {
        return findModeloEntities(false, maxResults, firstResult);
    }

    private List<Modelo> findModeloEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Modelo as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Modelo findModelo(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Modelo.class, id);
        } finally {
            em.close();
        }
    }

    public int getModeloCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Modelo as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
